package com.example.demospringboot;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.form.api.FormDeployment;
import org.flowable.form.api.FormInfo;
import org.flowable.form.api.FormRepositoryService;
import org.flowable.form.model.FormField;
import org.flowable.form.model.SimpleFormModel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhaoPin
 * @Date 2024/7/23  16:36
 * @description: 动态表单
 * 实际的工作流环境中是否要使用表单
 * 1.简单的数据我们可以使用表单
 * 2.复杂的数据我们直接使用业务主键即可
 */

@SpringBootTest
public class Flowable06Form {

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    FormRepositoryService formRepositoryService;

    @Autowired
    FormService formService;

    /**
     * 表单部署操作
     */
    @Test
    void deployForm() throws InterruptedException {
        FormDeployment formDeployment = formRepositoryService
                .createDeployment()
                .addClasspathResource("process/06-form/first.form")
                .name("报销表单")
                .deploy();

        System.out.println("-----<<<  formDeployment.getId() >>>------" + formDeployment.getId());
    }

    /**
     * 表单部署操作
     */
    @Test
    void deployForm02() throws InterruptedException {
        String jsonStr = "{\"name\":\"报销的表单3\",\"key\":\"formbx3\",\"version\":0,\"fields\":[{\"fieldType\":\"FormField\",\"id\":\"amount\",\"name\":\"报销金额\",\"type\":\"text\",\"value\":null,\"required\":true,\"readOnly\":false,\"overrideId\":true,\"placeholder\":\"0\",\"layout\":null},{\"fieldType\":\"FormField\",\"id\":\"reason\",\"name\":\"原因\",\"type\":\"text\",\"value\":null,\"required\":false,\"readOnly\":false,\"overrideId\":true,\"placeholder\":null,\"layout\":null},{\"fieldType\":\"FormField\",\"id\":\"bxDate\",\"name\":\"报销日期\",\"type\":\"date\",\"value\":null,\"required\":false,\"readOnly\":false,\"overrideId\":true,\"placeholder\":null,\"layout\":null}],\"outcomes\":[{\"id\":null,\"name\":\"Accept\"},{\"id\":null,\"name\":\"Reject\"}]}";
        FormDeployment formDeployment = formRepositoryService
                .createDeployment()
                // 名称后缀要以 .form 结尾，否则 act_fo_form_definition 表中不会对应的表单数据
                // json 中的 name 也会被使用存储到表中
                .addString("formbx3.form", jsonStr)
                .name("报销表单3")
                .deploy();

        System.out.println("-----<<<  formDeployment.getId() >>>------" + formDeployment.getId());
    }

    /**
     * 流程部署操作
     */
    @Test
    void deployFlow() throws InterruptedException {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/06-form/test-01.bpmn20.xml")
                .name("form-bx 流程")
                .deploy();// 部署的方法
        System.out.println("*******  deploy.getId() = " + deploy.getId());

    }

    /**
     * 执行绑定了表单的流程
     * 注意：表单名称中不能含有 "-" 和 "_"
     */
    @Test
    void startFlowWithForm() {
        String processDefinitionId = "form-demo3:3:6b3fd1a8-4a48-11ef-9c67-b469218e373e";
        Map<String, Object> map = new HashMap<>();
        map.put("amount", "8989");
        map.put("reason", "维护客户关系");
        map.put("bxDate", "2024-05-25");
        ProcessInstance processInstance = runtimeService.startProcessInstanceWithForm(processDefinitionId, null, map, "报销流程");
        System.out.println("*******  processInstance.getId() = " + processInstance.getId());
    }

    /**
     * 获取流程绑定的表单数据
     */
    @Test
    void getTaskFormInfo() {
        String processDefinitionId = "test-01:1:cec869f4-4a29-11ef-9a42-b469218e373e";
        String processInstanceId = "8ab791c4-4a2b-11ef-b74a-b469218e373e";
        FormInfo startFormModel = runtimeService.getStartFormModel(processDefinitionId, processInstanceId);
        // soutv
        System.out.println("*******  startFormModel.getKey() =" + startFormModel.getKey());
        System.out.println("*******  startFormModel.getName() =" + startFormModel.getName());
        System.out.println("*******  startFormModel.getDescription() =" + startFormModel.getDescription());

        // # 表单存储的数据
        // SELECT * from act_ru_variable ;

        // # 流程结束后进入到历史表中
        // SELECT * from act_hi_varinst ;
        SimpleFormModel formModel = (SimpleFormModel)startFormModel.getFormModel();
        final List<FormField> fields = formModel.getFields();
        for (FormField field : fields) {
            System.out.println("field.getId() = " + field.getId());
            System.out.println("field.getName() = " + field.getName());
            System.out.println("field.getValue() = " + field.getValue());
        }
    }

    /**
     * 任务的审批
     */
    @Test
    void completeTask() throws InterruptedException {
        Map<String, Object> map = new HashMap<>();
        map.put("flag",false);
        taskService.complete("f74eec30-4a48-11ef-9a08-b469218e373e",map);
        //Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 流程部署操作
     */
    @Test
    void deployFlowOutcome() throws InterruptedException {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/06-form/form-demo3.bpmn20.xml")
                .name("formbx3 流程")
                .deploy();// 部署的方法
        System.out.println("*******  deploy.getId() = " + deploy.getId());
    }

    /**
     * 任务的审批
     */
    @Test
    void completeTaskOutcome() throws InterruptedException {
        String taskId = "bf3b74bb-4a48-11ef-8133-b469218e373e";
        String formDefinitionId = "14b76aba-4a48-11ef-b7e1-b469218e373e";
        String outcome = "Reject";
        Map<String, Object> variables = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("amount", "777");
        map.put("reason", "维护客户关系777");
        map.put("bxDate", "2024-07-27");
        map.put("flag",false);
        taskService.completeTaskWithForm(taskId,formDefinitionId,outcome,map);
    }

}
